<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/9/29
 * Time: 19:58
 */
class Express_record_model extends CI_Model
{
    private $table = 'express_record';

    public function update($postData)
    {
        $data = array(
            'ischeck' => $postData->ischeck,
            'state'   => $postData->state,
            'context' => json_encode($postData->data),
        );
        $this->db->where('number', $postData->nu);
        $this->db->where('company', $postData->com);
        return $this->db->update($this->table, $data);
    }

    public function insert($name, $company, $number)
    {
        $data = array(
            'name'    => $name,
            'company' => $company,
            'number'  => $number,
            'ischeck' => -1,
            'state'   => -1,
            'context' => '',
            'create_at' => date('Y-m-d H:i:s')
        );
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    public function page_list($page_num, $num, $param)
    {
        $this->db->select('id,name,company,number,context,ischeck,state,create_at');
        $this->db->from($this->table);
        if(!empty($param['company'])){
            $this->db->like('company', $param['company']);
        }

        if(!empty($param['number'])){
            $this->db->where('number', $param['number']);
        }
        $this->db->order_by('id', 'DESC');
        $this->db->limit($page_num, $num);
        return $this->db->get();
    }

    public function total($param)
    {
        $this->db->select('*');
        $this->db->from($this->table);
        if(!empty($param['company'])){
            $this->db->like('company', $param['company']);
        }

        if(!empty($param['number'])){
            $this->db->where('number', $param['number']);
        }
        return $this->db->count_all_results();
    }

    public function findById($id)
    {
        $this->db->select('id,name,company,number,context,ischeck,state,create_at');
        $this->db->from($this->table);
        $this->db->where('id', $id);
        $this->db->limit(1);
        return $this->db->get();
    }
    
     public function findByNu($number)
    {
        $this->db->select('id,name,company,number,context,ischeck,state,create_at');
        $this->db->from($this->table);
        $this->db->where('number', $number);
        $this->db->limit(1);
        return $this->db->get();
    }
    
    public function findOrder()
    {
        $this->db->select("tourism_order.`logistics`,tourism_order.`logistics_company`,express_record.* ");
        $this->db->from('tourism_order AS tourism_order');
        $this->db->join($this->table.' AS express_record ', 'tourism_order.logistics = express_record.number AND tourism_order.logistics_company=express_record.company','left');
        $this->db->where('order_state', 2);
        $this->db->where('order_status > ', 3);

        return $this->db->get();
    }

}